package cn.deng.middleware.dynamic.thread.pool.sdk.config;

import cn.deng.middleware.dynamic.thread.pool.sdk.domain.DynamicThreadPoolService;
import com.alibaba.fastjson2.JSON;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: Dengjingwen deng
 * @description: 动态配置入口
 * @create: 2025-05-05
 */
@Configuration
public class DynamicThreadPoolAutoConfig {

    private final Logger logger = LoggerFactory.getLogger(DynamicThreadPoolAutoConfig.class);

    /**
     *
     * @Bean Spring容器在启动时会自动创建并管理这个Bean。
     *
     * @param applicationContext
     * @param threadPoolExecutorMap 扫描符合类型的bean对象，自动装配成线程池Map，其中键是bean名称，ThreadPoolExecutor是bean对象
     * @return
     */
    @Bean("dynamicThreadPoolService")
    public DynamicThreadPoolService  dynamicThreadPoolService(ApplicationContext applicationContext, Map<String, ThreadPoolExecutor> threadPoolExecutorMap){
        String applicationName = applicationContext.getEnvironment().getProperty("spring.application.name");

        if (StringUtils.isBlank(applicationName)){
            applicationName = "缺省的";
            logger.warn("SpringBoot 应用未配置");
        }

        return new DynamicThreadPoolService(applicationName, threadPoolExecutorMap);
    }

}
